package cn.tedu.douguo.user.controller;


import cn.tedu.douguo.commons.web.JsonResult;
import cn.tedu.douguo.user.pojo.dto.NewRecipeDTO;
import cn.tedu.douguo.user.pojo.vo.CategoryListVO;
import cn.tedu.douguo.user.pojo.vo.RecipeDetailsVO;
import cn.tedu.douguo.user.pojo.vo.RecipeListVO;
import cn.tedu.douguo.user.pojo.vo.RecipeStepVO;
import cn.tedu.douguo.user.service.CategoryService;
import cn.tedu.douguo.user.service.RecipeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/recipe")
@Slf4j
@Valid
@Api( tags = "一（￣︶￣）↗ 菜谱管理")
public class RecipeController {

    @Autowired
    private RecipeService service;

    //处理新增菜谱的请求
    @PostMapping("/addNewRecipe")
    @ApiOperation("新增菜谱")
    @ApiOperationSupport(order = 100)
    public JsonResult addNewRecipe(@RequestBody NewRecipeDTO newRecipeDTO){
        log.debug("开始处理【新增菜谱】的请求...，参数：{}",newRecipeDTO);
        service.insertRecipe(newRecipeDTO);
        return JsonResult.ok();
    }

    //处理展示8个菜谱的请求
    @GetMapping("/list/bySort")
    @ApiOperation("精选菜谱列表")
    @ApiOperationSupport(order = 401)
    public JsonResult recipeBySort(){
        log.debug("开始处理【精选菜谱列表】的请求...");
        List<RecipeListVO> list = service.listBySort();
        return JsonResult.ok(list);
    }

    //处理展示所有菜谱的请求
    @GetMapping("/list/all")
    @ApiOperation("所有菜谱列表")
    @ApiOperationSupport(order = 402)
    public JsonResult recipeAll(){
        log.debug("开始处理【所有菜谱列表】的请求...");
        List<RecipeListVO> list = service.listAll();
        return JsonResult.ok(list);
    }

    //处理展示所有菜谱的请求
    @GetMapping("/list/search")
    @ApiOperation("根据关键字展示菜谱列表")
    @ApiOperationSupport(order = 403)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词", required = true, dataType = "string")
    })
    public JsonResult recipeSearch(@RequestParam String keyword){
        log.debug("开始处理【根据关键字展示菜谱列表】的请求...，参数：{}",keyword);
        List<RecipeListVO> list = service.listByKeyword(keyword);
        return JsonResult.ok(list);
    }

    //处理展示单个菜谱的食材
    @GetMapping("/ingredients")
    @ApiOperation("食材列表")
    @ApiOperationSupport(order = 404)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recipeId", value = "菜谱id", required = true, dataType = "long")
    })
    public JsonResult recipeIngredients(Long recipeId){
        log.debug("开始处理【单个菜谱的食材】的请求...id:{}",recipeId);
        List<String> ingredients = service.getIngredientByRecipeId(recipeId);
        return JsonResult.ok(ingredients);
    }



    //处理展示步骤
    @GetMapping("/step")
    @ApiOperation("菜谱步骤")
    @ApiOperationSupport(order = 405)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recipeId", value = "菜谱id", required = true, dataType = "long")
    })
    public JsonResult recipeStep(Long recipeId){
        log.debug("开始处理【单个菜谱的步骤】的请求...");
        RecipeStepVO step = service.getStepByRecipeId(recipeId);
        return JsonResult.ok(step);
    }

    //处理展示单个菜谱
    @GetMapping("/details")
    @ApiOperation("菜谱")
    @ApiOperationSupport(order = 406)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recipeId", value = "菜谱id", required = true, dataType = "long")
    })
    public JsonResult recipe(Long recipeId){
        log.debug("开始处理【单个菜谱】的请求...");
        RecipeDetailsVO recipe = service.getDetailsByRecipeId(recipeId);
        return JsonResult.ok(recipe);
    }
}
